package com.ailk.cuc.autotestbg.common.utils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.logging.Log;

import com.ailk.cuc.autotestbg.common.bean.SysConfigBean;
import com.ailk.cuc.autotestbg.common.constants.StaticParam;
import com.ailk.cuc.autotestbg.common.exception.DataBaseException;
import com.ibatis.sqlmap.client.SqlMapClient;

public class InitUtils {

	private static Log logger = LogUtils.getLogger(InitUtils.class.getName());
	public static void initStaticParam(String configFile) throws DataBaseException{
		logger.info("加载系统配置文件config.cfg");
		PropUtils.init(configFile);
		
		logger.info("初始化系统参数ByConfig");
		initStaticParamByConfig();
		
		logger.info("初始化系统参数ByDB");
		initStaticParamByDB();
	}
	
	private static void initStaticParamByConfig(){
		StaticParam.port = Integer.parseInt(PropUtils.getValueByName("Port"));
    	StaticParam.url = PropUtils.getValueByName("Config_Url");
    	StaticParam.user = PropUtils.getValueByName("Config_User");
    	StaticParam.password = PropUtils.getValueByName("Config_Password");
    	StaticParam.clientNum = Integer.parseInt(PropUtils.getValueByName("Client_Num"));
    	StaticParam.threadNum = Integer.parseInt(PropUtils.getValueByName("Thread_Num"));
    	StaticParam.fieldMsgLength = Integer.parseInt(PropUtils.getValueByName("Field_Msg_Length"));
    	StaticParam.homeDir = PropUtils.getValueByName("Home_Dir");
    	StaticParam.testDataDir = PropUtils.getValueByName("Test_Data_Dir");
    	
    	StaticParam.zipFileDir = PropUtils.getValueByName("Zip_File_Dir");
    	StaticParam.unzipDir = PropUtils.getValueByName("Unzip_Dir");
    	StaticParam.cmd = PropUtils.getValueByName("Cmd");
    	StaticParam.agentClientNum = Integer.parseInt(PropUtils.getValueByName("Agent_Client_Num"));
    	StaticParam.agentPort = Integer.parseInt(PropUtils.getValueByName("Agent_Port"));
    	
    	StaticParam.reportImgDir = PropUtils.getValueByName("Report_Image_Dir");
    	StaticParam.reportDir = PropUtils.getValueByName("Report_Dir");
    	
    	StaticParam.fromMail = PropUtils.getValueByName("From_Mail");
    	StaticParam.addMail = new ArrayList<String>();
    	
    	String addMail = PropUtils.getValueByName("Add_Mail");

    	String[] addMailList = addMail.split(";");
    	for(int i=0;i<addMailList.length;i++) {
    		StaticParam.addMail.add(addMailList[i]);
    	}
    	
    	StaticParam.smtp = PropUtils.getValueByName("Smtp");
    	StaticParam.mailUser = PropUtils.getValueByName("Mail_User");
    	StaticParam.mailPasswd = PropUtils.getValueByName("Mail_Passwd");
    	StaticParam.ssl = PropUtils.getValueByName("SSL");
    	
    	if(StaticParam.ssl.equals("Y")) {
    		StaticParam.sslPort = PropUtils.getValueByName("SSL_Port");
    	}
    	
    	StaticParam.sendMail = PropUtils.getValueByName("Send_Mail");
    	
    	if (StaticParam.sendMail == null) {
    		StaticParam.sendMail = "N";
    	}
    	StaticParam.ftpMode = PropUtils.getValueByName("Ftp_Mode");
    	StaticParam.scriptEncoding = PropUtils.getValueByName("Script_Encoding");
    	StaticParam.systemInspectReportModel = PropUtils.getValueByName("System_Inspect_Report_Model");
    	
    	StaticParam.smsUrl = PropUtils.getValueByName("Sms_Url");
    	StaticParam.smsUser = PropUtils.getValueByName("Sms_User");
    	StaticParam.smsPassword = PropUtils.getValueByName("Sms_Password");
    	StaticParam.smsFactoryClass = PropUtils.getValueByName("Sms_Factory_Class");
    	
    	StaticParam.essXmlHead = PropUtils.getValueByName("Ess_Xml_Head");
    	StaticParam.xmlHeadPath = PropUtils.getValueByName("Ess_Xml_Path");
    	
    	StaticParam.ecsDBList = PropUtils.getValueByName("ECS_DB_LIST");
    	
    	StaticParam.socketCode = PropUtils.getValueByName("Socket_Code");
	}
	
	private static void initStaticParamByDB() throws DataBaseException{
		SqlMapClient sqlMapClient = (SqlMapClient) SpringContextUtil.getBean("sqlMapClient");
		String configName = null;
		String[] ecsDBArray = StaticParam.ecsDBList.split("#");
		SysConfigBean sysConfigBean = null;
		
		try {
			//初始化StaticParam.ecsDBInfoMap
			for(String ecsDBInstance : ecsDBArray){
				configName = ecsDBInstance;
				sysConfigBean = (SysConfigBean)sqlMapClient.queryForObject("common_init.getSysConfigBean", configName);
				if(StaticParam.ecsDBInfoMap == null){
					StaticParam.ecsDBInfoMap = new HashMap<String,String[]>();
				}
				String[] ecsDBInfo =sysConfigBean.getConfigValue().split("#");
				StaticParam.ecsDBInfoMap.put(ecsDBInstance, ecsDBInfo);
				
			}
			
			//初始化StaticParam.reportNameAdd
			configName = "BG_REPORT_ADD";
			sysConfigBean = (SysConfigBean)sqlMapClient.queryForObject("common_init.getSysConfigBean", configName);
			StaticParam.reportNameAdd = sysConfigBean.getConfigValue();
		} catch (SQLException e) {
			logger.error("初始化系统参数ByDB---出现异常");
			logger.error(e.getMessage());
			throw new DataBaseException("初始化系统参数ByDB---出现异常");
		}
	}
}
